package androidlab.sprintingsquirrel;

import java.util.HashSet;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.controller.MultiTouchController;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.debug.Debug;

import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import androidlab.sprintingsquirrel.sharing.ResultListener;

public class MainActivity extends BaseGameActivity {
	
	private BoundCamera camera;
	private DisplayMetrics metrics;
	private float aspectRatio;
	
	private HashSet<ResultListener> resultListeners;
	
	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
	    Engine engine = new LimitedFPSEngine(pEngineOptions, 60);
        engine.setTouchController(new MultiTouchController());
	    return engine;
	}
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		resultListeners = new HashSet<ResultListener>();
		metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		aspectRatio = metrics.heightPixels/(float) metrics.widthPixels;
		Debug.d("ratio: " + aspectRatio);
		camera = new BoundCamera(0, 0, 1280, 1280*aspectRatio);
		EngineOptions options = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
						1280, 1280*aspectRatio), camera);
		options.getAudioOptions().setNeedsMusic(true);
		options.getAudioOptions().setNeedsSound(true);
		options.getTouchOptions().setNeedsMultiTouch(true);
		options.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		return options;
	}

	@Override
	public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
		ResourceManager.initManager(mEngine, this, camera, getVertexBufferObjectManager(), metrics, 0.5625f);
		
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
		
		SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	}

	@Override
	public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		mEngine.registerUpdateHandler(new TimerHandler(2.0f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				mEngine.unregisterUpdateHandler(pTimerHandler);
				SceneManager.getInstance().createMenuScene();
			}
		}));
		
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
		}              
		return false;
	}

	@Override
	public synchronized void onResumeGame() {
//		if(sceneManager.getMusic() != null && !sceneManager.getMusic().isPlaying()) {
//			sceneManager.getMusic().play();
//		}
		super.onResumeGame();
	}

	@Override
	public synchronized void onPauseGame() {
//		if(sceneManager.getMusic() != null && sceneManager.getMusic().isPlaying()) {
//			sceneManager.getMusic().pause();
//		}
		super.onPauseGame();
	}
	
	@Override
	protected void onDestroy() {
	    super.onDestroy();
	        
	    if (this.isGameLoaded()) {
	        System.exit(0);    
	    }
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 1:
			informListener(requestCode, resultCode, data);
			break;
		case 3:
			informListener(requestCode, resultCode, data);
			break;
		}
	}
	
	public void addResultListener(ResultListener listener) {
		resultListeners.add(listener);
	}
	
	public void removeResultListener(ResultListener listener) {
		resultListeners.remove(listener);
	}
	
	private void informListener(int requestCode, int resultCode, Intent data) {
		for(ResultListener listener : resultListeners) {
			listener.onResult(requestCode, resultCode, data);
		}
	}

}
